{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## QIFI ACCOUNT TEST"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "from QIFIAccount import QIFI_Account, ORDER_DIRECTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[0;31mInit signature:\u001b[0m\n",
       "\u001b[0mQIFI_Account\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0musername\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0mpassword\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0mmodel\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'SIM'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0mbroker_name\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'QAPaperTrading'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0mtrade_host\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'mgdb'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0minit_cash\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1000000\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0mtaskid\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'7b4fe76a-97e1-4b3e-b8b9-be9ddb6e1c44'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
       "\u001b[0;31mDocstring:\u001b[0m      <no docstring>\n",
       "\u001b[0;31mInit docstring:\u001b[0m\n",
       "Initial\n",
       "QIFI Account是一个基于 DIFF/ QIFI/ QAAccount后的一个实盘适用的Account基类\n",
       "\n",
       "\n",
       "1. 兼容多持仓组合\n",
       "2. 动态计算权益\n",
       "\n",
       "使用 model = SIM/ REAL来切换\n",
       "\n",
       "qifiaccount 不去区分你的持仓是股票还是期货, 因此你可以实现跨市场的交易持仓管理\n",
       "\u001b[0;31mFile:\u001b[0m           ~/QIFIAccount/QIFIAccount/QAQIFIAccount.py\n",
       "\u001b[0;31mType:\u001b[0m           type\n",
       "\u001b[0;31mSubclasses:\u001b[0m     QIFI_StockSIM_Account\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "QIFI_Account?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "myacc =  QIFI_Account(username='myacc1', password='myacc1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Create new Account\n"
     ]
    }
   ],
   "source": [
    "myacc.initial()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 我们已经通过 initial 初始化了一个账户, 现在我们来查看下账户的基础信息\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "持仓\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "print('持仓')\n",
    "print(myacc.positions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "订单\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "print('订单')\n",
    "print(myacc.orders)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "成交单\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "print('成交单')\n",
    "print(myacc.trades)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "银行信息\n",
      "{'QASIM': {'id': 'QASIM', 'name': 'QASIMBank', 'bank_account': '', 'fetch_amount': 0.0, 'qry_count': 0}}\n"
     ]
    }
   ],
   "source": [
    "print('银行信息')\n",
    "print(myacc.banks)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账户基础信息\n",
      "{'user_id': 'myacc1', 'currency': 'CNY', 'pre_balance': 0, 'deposit': 1000000, 'withdraw': 0, 'WithdrawQuota': 0, 'close_profit': 0, 'commission': 0, 'premium': 0, 'static_balance': 0, 'position_profit': 0, 'float_profit': 0, 'balance': 1000000, 'margin': 0, 'frozen_margin': 0, 'frozen_commission': 0.0, 'frozen_premium': 0.0, 'available': 1000000, 'risk_ratio': 0.0}\n",
      "账户基础信息中包含了账户需要的所有数据, 具体可以参见 qifi 协议\n"
     ]
    }
   ],
   "source": [
    "print('账户基础信息')\n",
    "print(myacc.account_msg)\n",
    "\n",
    "\n",
    "print('账户基础信息中包含了账户需要的所有数据, 具体可以参见 qifi 协议')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 当我们需要知道账户的信息时, 我们只需要快速的去重新执行myacc.account_msg, 账户会惰性计算, 而当我们需要完整的账户切面, 我们会返回一个 QIFI SLICE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'account_cookie': 'myacc1',\n",
       " 'password': 'myacc1',\n",
       " 'databaseip': '',\n",
       " 'model': 'SIM',\n",
       " 'ping_gap': 5,\n",
       " 'portfolio': 'QAPaperTrade',\n",
       " 'broker_name': 'QAPaperTrading',\n",
       " 'capital_password': '',\n",
       " 'bank_password': '',\n",
       " 'bankid': 'QASIM',\n",
       " 'investor_name': '',\n",
       " 'money': 1000000,\n",
       " 'pub_host': '',\n",
       " 'trade_host': '',\n",
       " 'taskid': '7b4fe76a-97e1-4b3e-b8b9-be9ddb6e1c44',\n",
       " 'sourceid': 'QIFI_Account',\n",
       " 'updatetime': '',\n",
       " 'wsuri': 'ws://www.yutiansut.com:7988',\n",
       " 'bankname': 'QASIMBank',\n",
       " 'trading_day': '2020-08-06',\n",
       " 'status': 200,\n",
       " 'accounts': {'user_id': 'myacc1',\n",
       "  'currency': 'CNY',\n",
       "  'pre_balance': 0,\n",
       "  'deposit': 1000000,\n",
       "  'withdraw': 0,\n",
       "  'WithdrawQuota': 0,\n",
       "  'close_profit': 0,\n",
       "  'commission': 0,\n",
       "  'premium': 0,\n",
       "  'static_balance': 0,\n",
       "  'position_profit': 0,\n",
       "  'float_profit': 0,\n",
       "  'balance': 1000000,\n",
       "  'margin': 0,\n",
       "  'frozen_margin': 0,\n",
       "  'frozen_commission': 0.0,\n",
       "  'frozen_premium': 0.0,\n",
       "  'available': 1000000,\n",
       "  'risk_ratio': 0.0},\n",
       " 'trades': {},\n",
       " 'positions': {},\n",
       " 'orders': {},\n",
       " 'event': {'2020-08-06 00:50:30_529556': '转账成功 1000000'},\n",
       " 'transfers': {'0': {'datetime': 433241234123,\n",
       "   'currency': 'CNY',\n",
       "   'amount': 1000000,\n",
       "   'error_id': 0,\n",
       "   'error_msg': '成功'}},\n",
       " 'banks': {'QASIM': {'id': 'QASIM',\n",
       "   'name': 'QASIMBank',\n",
       "   'bank_account': '',\n",
       "   'fetch_amount': 0.0,\n",
       "   'qry_count': 0}},\n",
       " 'frozen': {},\n",
       " 'settlement': {}}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myacc.message"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "在实时模拟中, 获取最新的时间采用的是:\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'2020-08-06 00:56:02_131258'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('在实时模拟中, 获取最新的时间采用的是:')\n",
    "\n",
    "myacc.dtstr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在你已经有了一个基础的了解, 让我们继续往下看"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 下单函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'volume_long': 0, 'volume_short': 0, 'volume_long_frozen': 0, 'volume_short_frozen': 0}\n",
      "{'volume_long': 0, 'volume_short': 0}\n",
      "order check success\n",
      "下单成功 04e6e2c0-61dc-4d04-bca0-97bced2956b1\n"
     ]
    }
   ],
   "source": [
    "  \n",
    "r = myacc.send_order('000001', 10, 12, ORDER_DIRECTION.BUY)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'04e6e2c0-61dc-4d04-bca0-97bced2956b1': {'account_cookie': 'myacc1',\n",
       "  'user_id': 'myacc1',\n",
       "  'instrument_id': '000001',\n",
       "  'towards': 1,\n",
       "  'exchange_id': None,\n",
       "  'order_time': '2020-08-06 00:57:39_053237',\n",
       "  'volume': 10,\n",
       "  'price': 12.0,\n",
       "  'order_id': '04e6e2c0-61dc-4d04-bca0-97bced2956b1',\n",
       "  'seqno': 1,\n",
       "  'direction': 'BUY',\n",
       "  'offset': '',\n",
       "  'volume_orign': 10,\n",
       "  'price_type': 'LIMIT',\n",
       "  'limit_price': 12.0,\n",
       "  'time_condition': 'GFD',\n",
       "  'volume_condition': 'ANY',\n",
       "  'insert_date_time': 1.596646659052434e+18,\n",
       "  'exchange_order_id': '2ba5fba7-c8cb-4d59-8074-24cd3c7b6e78',\n",
       "  'status': 'ALIVE',\n",
       "  'volume_left': 10,\n",
       "  'last_msg': '已报'}}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myacc.orders"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myacc.trades"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 撮合订单"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "全部成交 04e6e2c0-61dc-4d04-bca0-97bced2956b1\n",
      "update trade\n"
     ]
    }
   ],
   "source": [
    "import uuid\n",
    "myacc.receive_deal(r['instrument_id'], 11.8, r['volume'], r['towards'],\n",
    "                  myacc.dtstr, order_id=r['order_id'], trade_id=str(uuid.uuid4()))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'58f04d40-c126-4539-9a6b-06b5c5a09c79': {'seqno': 2,\n",
       "  'user_id': 'myacc1',\n",
       "  'trade_id': '58f04d40-c126-4539-9a6b-06b5c5a09c79',\n",
       "  'exchange_id': None,\n",
       "  'instrument_id': '000001',\n",
       "  'order_id': '04e6e2c0-61dc-4d04-bca0-97bced2956b1',\n",
       "  'exchange_trade_id': '58f04d40-c126-4539-9a6b-06b5c5a09c79',\n",
       "  'direction': 'BUY',\n",
       "  'offset': '',\n",
       "  'volume': 10,\n",
       "  'price': 11.8,\n",
       "  'trade_time': '2020-08-06 00:58:41_440478',\n",
       "  'commission': 0.0,\n",
       "  'trade_date_time': 1.5966467214404782e+18}}"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myacc.trades"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'04e6e2c0-61dc-4d04-bca0-97bced2956b1': {'account_cookie': 'myacc1',\n",
       "  'user_id': 'myacc1',\n",
       "  'instrument_id': '000001',\n",
       "  'towards': 1,\n",
       "  'exchange_id': None,\n",
       "  'order_time': '2020-08-06 00:57:39_053237',\n",
       "  'volume': 10,\n",
       "  'price': 12.0,\n",
       "  'order_id': '04e6e2c0-61dc-4d04-bca0-97bced2956b1',\n",
       "  'seqno': 1,\n",
       "  'direction': 'BUY',\n",
       "  'offset': '',\n",
       "  'volume_orign': 10,\n",
       "  'price_type': 'LIMIT',\n",
       "  'limit_price': 12.0,\n",
       "  'time_condition': 'GFD',\n",
       "  'volume_condition': 'ANY',\n",
       "  'insert_date_time': 1.596646659052434e+18,\n",
       "  'exchange_order_id': '2ba5fba7-c8cb-4d59-8074-24cd3c7b6e78',\n",
       "  'status': 'FINISHED',\n",
       "  'volume_left': 0,\n",
       "  'last_msg': '全部成交'}}"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myacc.orders"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
